﻿using Microsoft.Xna.Framework;
using System.Collections.Generic;
//using Microsoft.Xna.Framework.Input;
using CsharpPoker.Core;

namespace CsharpPoker
{
    /// <summary>
    /// Manages CSP's scenes. Contains an explicit list of scenes and navigates between them.
    /// </summary>
    public class SceneManager : DrawableGameComponent
    {
        private readonly List<GameComponent> components;

        private GameScene activeScene;

        private Game game; // reference to my Game

        private StartScene startScene;
        private TexasHoldemScene texasHoldemScene;

        public SceneManager(Game game)
            : base (game)
        {
            this.game = game;
            components = new List<GameComponent>();
        }

        /// <summary>
        /// Creats and initializes all scenes and actives the starting scene.
        /// </summary>
        public override void Initialize()
        {
            startScene = new StartScene(game, this);
            components.Add(startScene);

            texasHoldemScene = new TexasHoldemScene(game, this);
            components.Add(texasHoldemScene);

            foreach (GameComponent c in components)
            {
                c.Initialize();
            }

            ShowScene(startScene);

            base.Initialize();
        }

        protected override void LoadContent()
        {
            base.LoadContent();
        }

        public virtual void Input()
        {
            activeScene.Input();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            Input();

            // Update the child GameComponents
            for (int i = 0; i < components.Count; i++)
            {
                if (components[i].Enabled)
                {
                    components[i].Update(gameTime);
                }
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            // Draw the child GameComponents (if drawable)
            for (int i = 0; i < components.Count; i++)
            {
                GameComponent gc = components[i];
                if ((gc is DrawableGameComponent) && 
                    ((DrawableGameComponent) gc).Visible)
                {
                    ((DrawableGameComponent) gc).Draw(gameTime);
                }
            }
            base.Draw(gameTime);
        }

        /// <summary>
        /// Open a new scene
        /// </summary>
        /// <param name="scene">Scene to be opened</param>
        public void ShowScene(GameScene scene)
        {
            if (activeScene != null)
            {
                activeScene.Hide();
            }
            activeScene = scene;
            scene.Show();
        }

        // accessors ------------------------------------------------------- //

        public List<GameComponent> Components
        {
            get { return components; }
        }

        public StartScene StartScene
        {
            get { return startScene; }
        }

        public TexasHoldemScene TexasHoldem
        {
            get { return texasHoldemScene; }
        }
    }
}